##################################################
#
# TABLES OF NEW INDICES FOR
# OIL, ISLAM, and WOMEN PROJECT
#
# Paul Musgrave and Yu-Ming Liou
# musgrave@umass.edu and yl254@georgetown.edu
#
# Created 27 February 2016
#
##################################################


##################################################
# R Housekeeping
##################################################

# Remove and empty workspace
rm(list=ls())

#  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  # 
# Load Packages
#  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  # 

library(foreign)
library(sandwich)
library(apsrtable)
library(stargazer)
library(tonymisc)  # includes summaryR()
library(MASS) # for polr() which runs our ologit

#  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  # 
# Load Functions
#  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  # 

# Change this to  your working directory
mywd <- "/Users/paulmusgrave/Dropbox/0001 Academic Projects/Ongoing/0127 Oil Islam Women/ISQ Accepted Submission/Replication"

setwd(paste(mywd,"/Code",sep=""))


source("statifyFN.R")
source("multipleOLSFN.R")
source("genModelsFN.R")
source("plotlineFN.R")
source("plotprepFN.R")
source("modelDefinitions.R")
source("tableOLSFN.R")
source("prepper.R")
source("helperFN.R")
source("tableOlogit.R")
tablenames <- read.csv("allmodels.csv", stringsAsFactors=FALSE)

#  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  # 
# Load Data
#  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  # 

setwd(paste(mywd,"/Data",sep=""))

data <- read.dta("NewDVModelsXSforISQFINAL2015.dta")


# NOTE THAT FACTORS HAVE BEEN REORDERED SO THAT
# WORSE OUTCOMES FOR WOMEN ARE CONSISTENTLY ** LOWER **

data$ciri_wecon_02 <- factor(data$ciri_wecon_02,
                             levels=c(0,1,2,3),
                             ordered=TRUE)
data$ciri_wopol_02 <- factor(data$ciri_wopol_02,
                             levels=c(0,1,2,3),
                             ordered=TRUE)
data$ciri_wosoc_02 <- factor(data$ciri_wosoc_02,
                             levels=c(0,1,2,3),
                             ordered=TRUE)

data$ciri_wecon_04 <- factor(data$ciri_wecon_04,
                             levels=c(0,1,2,3),
                             ordered=TRUE)
data$ciri_wopol_04 <- factor(data$ciri_wopol_04,
                             levels=c(0,1,2,3),
                             ordered=TRUE)
data$ciri_wosoc_04 <- factor(data$ciri_wosoc_04,
                             levels=c(0,1,2,3),
                             ordered=TRUE)

data$McDermott_07 <- factor(data$McDermott_07,
                            #                           levels=c(0,1,2,3,4),
                            levels=c(4:0),
                            ordered=TRUE)
data$Caprioli_07 <- factor(data$Caprioli_07,
                           #                           levels=c(0,1,2,3,4),
                           levels=c(4:0),
                           ordered=TRUE)

data$McDermott_11 <- factor(data$McDermott_11,
                            #                           levels=c(0,1,2,3,4),
                            levels=c(4:0),
                            ordered=TRUE)
data$Brinton_10 <- factor(data$Brinton_10,
                          #                          levels=c(1:4),
                          levels=c(4:1),
                          ordered=TRUE)

data$Clan.fac<- factor(data$Clan,
                       #                          levels=c(1:12),
                       levels=c(12:1),
                       ordered=TRUE)

data$GII_UN_11 <- -(data$GII_UN_11) # to reorder UN so everything is negative


data$oil_gas_pc_11_log <- log(data$oil_gas_pc_11+1)
data$oil_gas_pc_07_log <- log(data$oil_gas_pc_07+1)
data$oil_gas_pc_04_log <- log(data$oil_gas_pc_04+1)
data$oil_gas_pc_02_log <- log(data$oil_gas_pc_02+1)


data$oil_gas_pc_11 <- data$oil_gas_pc_11 / 1000
data$oil_gas_pc_07 <- data$oil_gas_pc_07 / 1000
data$oil_gas_pc_04 <- data$oil_gas_pc_04 / 1000
data$oil_gas_pc_02 <- data$oil_gas_pc_02 / 1000

setwd(paste(mywd,"/Drafts/Tables",sep=""))



#################################################
#
# Models of Oil Income and Vice Issues
# 
#################################################

# UN GII
tab.GII.ols.1 <- tableOLS(formulas=genModels(c("GII_UN_11"),
                                           c(p140.11)))
tab.GII.ols.2 <- tableOLS(formulas=genModels(c("GII_UN_11"),
                                             c(p142.11)))

stargazer(tab.GII.ols.1,
          title= "Oil Income Per Capita and UN Gender Inequality Index",
          dep.var.labels = c("UN GII"),
          covariate.labels=c(p140.11.names[,2],"Intercept"),
          out="ISQFINALAppendixTable43.tex",
          style="apsr",
          label="tab:ungii1",
          no.space=TRUE,
          omit.stat=c("ser","f"))

stargazer(tab.GII.ols.2,
          title= "Oil Income Per Capita and UN Gender Inequality Index",
          dep.var.labels = c("UN GII"),
          covariate.labels=c(p142.11.names[,2],"Intercept"),
          out="ISQFINALAppendixTable44.tex",
          style="apsr",
          label="tab:ungii2",
          no.space=TRUE,
          omit.stat=c("ser","f"))

# CIRI WOPOL



# McDermott
tab.McDermott_07.1<- tableOlogit(formulas=genModels(c("McDermott_07"),
                                                     c(p140.07)),
                                  y=data)
stargazer(tab.McDermott_07.1,
          title= "Oil Income Per Capita and McDermott's Index 2007",
          dep.var.labels = c("McDermott's Family Law Index 2007"),
          covariate.labels=c(p140.07.names[,2],"Intercept"),
          out="ISQFINALAppendixTable39.tex",
          style="apsr",
          label="tab:mcdermott071",
          no.space=TRUE,
          omit.stat=c("ser","f"),
          model.names=TRUE,
          column.labels=c(LETTERS[1:length(tab.McDermott_07.1)]))

tab.McDermott_07.2<- tableOlogit(formulas=genModels(c("McDermott_07"),
                                                    c(p142.07)),
                                 y=data)
stargazer(tab.McDermott_07.2,
          title= "Oil Income Per Capita and McDermott's Index 2007",
          dep.var.labels = c("McDermott's Family Law Index 2007"),
          covariate.labels=c(p142.07.names[,2],"Intercept"),
          out="ISQFINALAppendixTable40.tex",
          style="apsr",
          label="tab:mcdermott072",
          no.space=TRUE,
          omit.stat=c("ser","f"),
          model.names=TRUE,
          column.labels=c(LETTERS[1:length(tab.McDermott_07.2)]))

# Caprioli
tab.Caprioli_07.1 <- tableOlogit(formulas=genModels(c("Caprioli_07"),
                                                    c(p140.07)),
                                 y=data)
stargazer(tab.Caprioli_07.1,
          title= "Oil Income Per Capita and Caprioli's Index",
          dep.var.labels = c("Caprioli's Index 2007"),
          covariate.labels=c(p140.07.names[,2],"Intercept"),
          out="ISQFINALAppendixTable35.tex",
          style="apsr",
          label="tab:caprioli11",
          no.space=TRUE,
          omit.stat=c("ser","f"),
          model.names=TRUE,
          column.labels=c(LETTERS[1:length(tab.Caprioli_07.1)]))

tab.Caprioli_07.2 <- tableOlogit(formulas=genModels(c("Caprioli_07"),
                                                  c(p142.07)),
                               y=data)
stargazer(tab.Caprioli_07.2,
          title= "Oil Income Per Capita and Caprioli's Index",
          dep.var.labels = c("Caprioli's Index 2007"),
          covariate.labels=c(p142.07.names[,2],"Intercept"),
          out="ISQFINALAppendixTable36.tex",
          style="apsr",
          label="tab:caprioli12",
          no.space=TRUE,
          omit.stat=c("ser","f"),
          model.names=TRUE,
          column.labels=c(LETTERS[1:length(tab.Caprioli_07.2)]))

# Brinton
# Brinton
tab.Brinton_10.1 <- tableOlogit(formulas=genModels(c("Brinton_10"),
                                                   c(p140.11)),
                                y=data)
stargazer(tab.Brinton_10.1,
          title= "Oil Income Per Capita and Brinton's Index",
          dep.var.labels = c("Brinton's Index 2011"),
          covariate.labels=c(p140.11.names[,2],"Intercept"),
          out="ISQFINALAppendixTable37.tex",
          style="apsr",
          label="tab:Brinton11",
          no.space=TRUE,
          omit.stat=c("ser","f"),
          model.names=TRUE,
          column.labels=c(LETTERS[1:length(tab.Brinton_10.1)]))

tab.Brinton_10.2 <- tableOlogit(formulas=genModels(c("Brinton_10"),
                                                   c(p142.11)),
                                y=data)
stargazer(tab.Brinton_10.2,
          title= "Oil Income Per Capita and Brinton's Index",
          dep.var.labels = c("Brinton's Index 2011"),
          covariate.labels=c(p142.11.names[,2],"Intercept"),
          out="ISQFINALAppendixTable38.tex",
          style="apsr",
          label="tab:Brinton12",
          no.space=TRUE,
          omit.stat=c("ser","f"),
          model.names=TRUE,
          column.labels=c(LETTERS[1:length(tab.Brinton_10.2)]))

# McDermott
tab.McDermott_11.1<- tableOlogit(formulas=genModels(c("McDermott_11"),
                                                    c(p140.11)),
                                 y=data)
stargazer(tab.McDermott_11.1,
          title= "Oil Income Per Capita and McDermott's Index 2011",
          dep.var.labels = c("McDermott's Family Law Index 2011"),
          covariate.labels=c(p140.11.names[,2],"Intercept"),
          out="ISQFINALAppendixTable41.tex",
          style="apsr",
          label="tab:mcdermott111",
          no.space=TRUE,
          omit.stat=c("ser","f"),
          model.names=TRUE,
          column.labels=c(LETTERS[1:length(tab.McDermott_11.1)]))

tab.McDermott_11.2<- tableOlogit(formulas=genModels(c("McDermott_11"),
                                                    c(p142.11)),
                                 y=data)
stargazer(tab.McDermott_11.2,
          title= "Oil Income Per Capita and McDermott's Index 2011",
          dep.var.labels = c("McDermott's Family Law Index 2011"),
          covariate.labels=c(p142.11.names[,2],"Intercept"),
          out="ISQFINALAppendixTable42.tex",
          style="apsr",
          label="tab:mcdermott112",
          no.space=TRUE,
          omit.stat=c("ser","f"),
          model.names=TRUE,
          column.labels=c(LETTERS[1:length(tab.McDermott_11.2)]))

# Clan
tab.Clan.fac.1 <- tableOlogit(formulas=genModels(c("Clan.fac"),
                                                 c(p140.11)),
                              y=data)
stargazer(tab.Clan.fac.1,
          title= "Oil Income Per Capita and Hudson et al Clan Index",
          dep.var.labels = c("Hudson et al Clan Index"),
          covariate.labels=c(p140.11.names[,2],"Intercept"),
          out="ISQFINALAppendixTable45.tex",
          style="apsr",
          label="tab:Clan11",
          no.space=TRUE,
          omit.stat=c("ser","f"),
          model.names=TRUE,
          column.labels=c(LETTERS[1:length(tab.Clan.fac.1)]))

tab.Clan.fac.2 <- tableOlogit(formulas=genModels(c("Clan.fac"),
                                                 c(p142.11)),
                              y=data)
stargazer(tab.Clan.fac.2,
          title= "Oil Income Per Capita and Hudson et al Clan Index",
          dep.var.labels = c("Hudson et al Clan Index"),
          covariate.labels=c(p142.11.names[,2],"Intercept"),
          out="ISQFINALAppendixTable46.tex",
          style="apsr",
          label="tab:Clan12",
          no.space=TRUE,
          omit.stat=c("ser","f"),
          model.names=TRUE,
          column.labels=c(LETTERS[1:length(tab.Clan.fac.2)]))


# CIRI WECON
tab.ciri_wecon_04.1 <- tableOlogit(formulas=genModels(c("ciri_wecon_04"),
                                                      c(p140.04)),
                                   y=data)
stargazer(tab.ciri_wecon_04.1,
          title= "Oil Income Per Capita and Cingranelli-Richards Economics Index 2004",
          dep.var.labels = c("Cingranelli-Richards Economics Index 2004"),
          covariate.labels=c(p140.04.names[,2],"Intercept"),
          out="ISQFINALAppendixTable33.tex",
          style="apsr",
          label="tab:CIRIwecon041",
          no.space=TRUE,
          omit.stat=c("ser","f"),
          model.names=TRUE,
          column.labels=c(LETTERS[1:length(tab.ciri_wecon_04.1)]))

tab.ciri_wecon_04.2 <- tableOlogit(formulas=genModels(c("ciri_wecon_04"),
                                                      c(p142.04)),
                                   y=data)
stargazer(tab.ciri_wecon_04.2,
          title= "Oil Income Per Capita and Cingranelli-Richards Economics Index 2004",
          dep.var.labels = c("Cingranelli-Richards Economics Index 2004"),
          covariate.labels=c(p142.04.names[,2],"Intercept"),
          out="ISQFINALAppendixTable34.tex",
          style="apsr",
          label="tab:CIRIwecon042",
          no.space=TRUE,
          omit.stat=c("ser","f"),
          model.names=TRUE,
          column.labels=c(LETTERS[1:length(tab.ciri_wecon_04.2)]))

tab.ciri_wecon_02.1 <- tableOlogit(formulas=genModels(c("ciri_wecon_02"),
                                                      c(p140.02)),
                                   y=data)
stargazer(tab.ciri_wecon_02.1,
          title= "Oil Income Per Capita and Cingranelli-Richards Economics Index 2002",
          dep.var.labels = c("Cingranelli-Richards Economics Index 2002"),
          covariate.labels=c(p140.02.names[,2],"Intercept"),
          out="ISQFINALAppendixTable31.tex",
          style="apsr",
          label="tab:CIRIwecon021",
          no.space=TRUE,
          omit.stat=c("ser","f"),
          model.names=TRUE,
          column.labels=c(LETTERS[1:length(tab.ciri_wecon_02.1)]))

tab.ciri_wecon_02.2 <- tableOlogit(formulas=genModels(c("ciri_wecon_02"),
                                                      c(p142.02)),
                                   y=data)
stargazer(tab.ciri_wecon_02.2,
          title= "Oil Income Per Capita and Cingranelli-Richards Economics Index 2002",
          dep.var.labels = c("Cingranelli-Richards Economics Index 2002"),
          covariate.labels=c(p142.02.names[,2],"Intercept"),
          out="ISQFINALAppendixTable32.tex",
          style="apsr",
          label="tab:CIRIwecon022",
          no.space=TRUE,
          omit.stat=c("ser","f"),
          model.names=TRUE,
          column.labels=c(LETTERS[1:length(tab.ciri_wecon_02.2)]))

# CIRI WOPOL
# CIRI wopol
tab.ciri_wopol_04.1 <- tableOlogit(formulas=genModels(c("ciri_wopol_04"),
                                                      c(p140.04)),
                                   y=data)
stargazer(tab.ciri_wopol_04.1,
          title= "Oil Income Per Capita and Cingranelli-Richards Politics Index 2004",
          dep.var.labels = c("Cingranelli-Richards Politics Index 2004"),
          covariate.labels=c(p140.04.names[,2],"Intercept"),
          out="ISQFINALAppendixTable29.tex",
          style="apsr",
          label="tab:CIRIwopol041",
          no.space=TRUE,
          omit.stat=c("ser","f"),
          model.names=TRUE,
          column.labels=c(LETTERS[1:length(tab.ciri_wopol_04.1)]))

tab.ciri_wopol_04.2 <- tableOlogit(formulas=genModels(c("ciri_wopol_04"),
                                                      c(p142.04)),
                                   y=data)
stargazer(tab.ciri_wopol_04.2,
          title= "Oil Income Per Capita and Cingranelli-Richards Politics Index 2004",
          dep.var.labels = c("Cingranelli-Richards Politics Index 2004"),
          covariate.labels=c(p142.04.names[,2],"Intercept"),
          out="ISQFINALAppendixTable30.tex",
          style="apsr",
          label="ISQFINALAppendixTable30.tex",
          no.space=TRUE,
          omit.stat=c("ser","f"),
          model.names=TRUE,
          column.labels=c(LETTERS[1:length(tab.ciri_wopol_04.2)]))

tab.ciri_wopol_02.1 <- tableOlogit(formulas=genModels(c("ciri_wopol_02"),
                                                      c(p140.02)),
                                   y=data)
stargazer(tab.ciri_wopol_02.1,
          title= "Oil Income Per Capita and Cingranelli-Richards Politics Index 2002",
          dep.var.labels = c("Cingranelli-Richards Politics Index 2002"),
          covariate.labels=c(p140.02.names[,2],"Intercept"),
          out="ISQFINALAppendixTable27.tex",
          style="apsr",
          label="tab:CIRIwopol021",
          no.space=TRUE,
          omit.stat=c("ser","f"),
          model.names=TRUE,
          column.labels=c(LETTERS[1:length(tab.ciri_wopol_02.1)]))

tab.ciri_wopol_02.2 <- tableOlogit(formulas=genModels(c("ciri_wopol_02"),
                                                      c(p142.02)),
                                   y=data)
stargazer(tab.ciri_wopol_02.2,
          title= "Oil Income Per Capita and Cingranelli-Richards Politics Index 2002",
          dep.var.labels = c("Cingranelli-Richards Politics Index 2002"),
          covariate.labels=c(p142.02.names[,2],"Intercept"),
          out="ISQFINALAppendixTable28.tex",
          style="apsr",
          label="tab:CIRIwopol022",
          no.space=TRUE,
          omit.stat=c("ser","f"),
          model.names=TRUE,
          column.labels=c(LETTERS[1:length(tab.ciri_wopol_02.2)]))

# CIRI WOSOC
# CIRI wosoc
tab.ciri_wosoc_04.1 <- tableOlogit(formulas=genModels(c("ciri_wosoc_04"),
                                                      c(p140.04)),
                                   y=data)
stargazer(tab.ciri_wosoc_04.1,
          title= "Oil Income Per Capita and Cingranelli-Richards Social Index 2004",
          dep.var.labels = c("Cingranelli-Richards Social Index 2004"),
          covariate.labels=c(p140.04.names[,2],"Intercept"),
          out="ISQFINALAppendixTable25.tex",
          style="apsr",
          label="tab:CIRIwosoc041",
          no.space=TRUE,
          omit.stat=c("ser","f"),
          model.names=TRUE,
          column.labels=c(LETTERS[1:length(tab.ciri_wosoc_04.1)]))

tab.ciri_wosoc_04.2 <- tableOlogit(formulas=genModels(c("ciri_wosoc_04"),
                                                      c(p142.04)),
                                   y=data)
stargazer(tab.ciri_wosoc_04.2,
          title= "Oil Income Per Capita and Cingranelli-Richards Social Index 2004",
          dep.var.labels = c("Cingranelli-Richards Social Index 2004"),
          covariate.labels=c(p142.04.names[,2],"Intercept"),
          out="ISQFINALAppendixTable26.tex",
          style="apsr",
          label="tab:CIRIwosoc042",
          no.space=TRUE,
          omit.stat=c("ser","f"),
          model.names=TRUE,
          column.labels=c(LETTERS[1:length(tab.ciri_wosoc_04.2)]))

tab.ciri_wosoc_02.1 <- tableOlogit(formulas=genModels(c("ciri_wosoc_02"),
                                                      c(p140.02)),
                                   y=data)
stargazer(tab.ciri_wosoc_02.1,
          title= "Oil Income Per Capita and Cingranelli-Richards Social Index 2002",
          dep.var.labels = c("Cingranelli-Richards Social Index 2002"),
          covariate.labels=c(p140.02.names[,2],"Intercept"),
          out="ISQFINALAppendixTable23.tex",
          style="apsr",
          label="tab:CIRIwosoc021",
          no.space=TRUE,
          omit.stat=c("ser","f"),
          model.names=TRUE,
          column.labels=c(LETTERS[1:length(tab.ciri_wosoc_02.1)]))

tab.ciri_wosoc_02.2 <- tableOlogit(formulas=genModels(c("ciri_wosoc_02"),
                                                      c(p142.02)),
                                   y=data)
stargazer(tab.ciri_wosoc_02.2,
          title= "Oil Income Per Capita and Cingranelli-Richards Social Index 2002",
          dep.var.labels = c("Cingranelli-Richards Social Index 2002"),
          covariate.labels=c(p142.02.names[,2],"Intercept"),
          out="ISQFINALAppendixTable24.tex",
          style="apsr",
          label="tab:CIRIwosoc022",
          no.space=TRUE,
          omit.stat=c("ser","f"),
          model.names=TRUE,
          column.labels=c(LETTERS[1:length(tab.ciri_wosoc_02.2)]))


